package cn.lingyangwl.agile.misc.statistic.service;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.lingyangwl.agile.misc.statistic.mapper.*;
import cn.lingyangwl.agile.misc.statistic.model.entity.*;
import cn.lingyangwl.agile.misc.statistic.model.rqrs.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
public class StatQueryServiceImpl implements StatQueryService {
    @Resource
    private StatDayRecordMapper dayRecordMapper;


    @Override
    public List<ChartData> listAccessLog(StatBaseReq req) {
        LocalDate endData = LocalDate.now();
        LocalDate beginDate = LocalDate.now().plusDays(req.getDays() * -1L);
        List<StatDayRecord> list = dayRecordMapper.listAccessLog(beginDate, LocalDate.now());

        List<ChartData> respList = new ArrayList<>();
        list.forEach(e -> {
            respList.add(new ChartData(e.getStatDate(), e.getAccessFailCount()).setType("失败"));
            respList.add(new ChartData(e.getStatDate(), e.getAccessSuccessCount()).setType("成功"));
        });

        Map<String, List<ChartData>> chartMap = respList.stream().collect(Collectors.groupingBy(ChartData::getName));

        while (beginDate.isBefore(endData)) {
            String dateStr = LocalDateTimeUtil.format(beginDate, "yyyy-MM-dd");
            if (!chartMap.containsKey(dateStr)) {
                respList.add(new ChartData(dateStr, 0).setType("失败"));
                respList.add(new ChartData(dateStr, 0).setType("成功"));
            }
            beginDate = beginDate.plusDays(1);
        }

        return respList.stream().sorted(Comparator.comparing(ChartData::getName)).collect(Collectors.toList());
    }

    @Override
    public List<ChartData> listLogin(StatBaseReq req) {
        LocalDate endData = LocalDate.now();
        LocalDate beginDate = LocalDate.now().plusDays(req.getDays() * -1L);
        List<StatDayRecord> list = dayRecordMapper.listLogin(beginDate, endData);

        List<ChartData> respList = new ArrayList<>();
        list.forEach(e -> {
            respList.add(new ChartData(e.getStatDate(), e.getLoginFailCount() + e.getLoginSuccessCount()).setType("登录"));
            respList.add(new ChartData(e.getStatDate(), e.getLogoutCount()).setType("退出"));
        });

        Map<String, List<ChartData>> chartMap = respList.stream().collect(Collectors.groupingBy(ChartData::getName));

        while (beginDate.isBefore(endData)) {
            String dateStr = LocalDateTimeUtil.format(beginDate, "yyyy-MM-dd");
            if (!chartMap.containsKey(dateStr)) {
                respList.add(new ChartData(dateStr, 0).setType("登录"));
                respList.add(new ChartData(dateStr, 0).setType("退出"));
            }
            beginDate = beginDate.plusDays(1);
        }

        return respList.stream().sorted(Comparator.comparing(ChartData::getName)).collect(Collectors.toList());
    }
}
